﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem.DataAccess
{
    public class LogDataAccess : SqlProvider
    {
        private int Insert(SystemLogEntity logEntity, string name)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return 0;
            }

            int result = 0;
            try
            {
                using (var sqlCommand = new SqlCommand("sp_Log_Insert", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.AddWithValue("@logTypeId", logEntity.LogTypeId);
                    sqlCommand.Parameters.AddWithValue("@content", logEntity.Content);
                    sqlCommand.Parameters.AddWithValue("@userName", name);

                    result = sqlCommand.ExecuteNonQuery();
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
            }
            finally
            {
                //CloseConnection();
            }

            return result;
        }

        public DataTable FetchAll()
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return null;
            }

            DataTable result = null;
            try
            {
                using (var sqlCommand = new SqlCommand("sp_Log_Select", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                    var adapter = new SqlDataAdapter(sqlCommand);
                    result = new DataTable { Locale = CultureInfo.CurrentCulture };
                    adapter.Fill(result);
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
            }
            finally
            {
                CloseConnection();
            }

            return result;
        }

        public void WriteError(string message, string name)
        {
            var logEntity = new SystemLogEntity
                                {
                                    Content = message,
                                    LogTypeId = new Guid("58CFDAD1-B624-4B5F-BA96-FB07F1AD5D67")
                                };
            Insert(logEntity, name);
        }

        public void WriteEvent(string message, string name)
        {
            var logEntity = new SystemLogEntity
            {
                Content = message,
                LogTypeId = new Guid("1FE6C14F-2CE8-4971-B837-0638AF698786")
            };
            Insert(logEntity, name);
        }

        public DataTable ViewLog(int type)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return null;
            }

            DataTable dataTable = null;
            try
            {
                using (var sqlCommand = new SqlCommand("sp_Systemlog_Viewlog", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.Add(new SqlParameter("@type", type));
                    var adapter = new SqlDataAdapter(sqlCommand);
                    dataTable = new DataTable { Locale = CultureInfo.CurrentCulture };
                    adapter.Fill(dataTable);
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
                return dataTable;
            }
            catch (NullReferenceException nullReferenceException)
            {
                ErrorMessage = nullReferenceException.Message;
                return dataTable;
            }
            finally
            {
                CloseConnection();
            }

            return dataTable;
        }

        public DataTable SearchLog(int type, DateTime startDate, DateTime endDate)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return null;
            }

            DataTable dataTable = null;
            try
            {
                using (var sqlCommand = new SqlCommand("sp_Systemlog_SearchDate", CreateConnection()))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.Add(new SqlParameter("@type", type));
                    sqlCommand.Parameters.Add(new SqlParameter("@Fromdate", startDate));
                    sqlCommand.Parameters.Add(new SqlParameter("@Todate", endDate));
                    var adapter = new SqlDataAdapter(sqlCommand);
                    dataTable = new DataTable { Locale = CultureInfo.CurrentCulture };
                    adapter.Fill(dataTable);
                    ErrorMessage = null;
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
            }
            finally
            {
                CloseConnection();
            }

            return dataTable;
        }
    }
}
